/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.plugins.impl;

import java.util.List;

import org.grayrabbit.cms.dao.plugins.VoteDao;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.Vote;
import org.grayrabbit.cms.service.plugins.VoteService;
import org.grayrabbit.cms.vo.plugins.VoteBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 16:08:34  
 */
@Service
@Transactional
public class VoteServiceImpl extends GenericServiceImpl implements VoteService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("voteDaoImpl")
	private VoteDao voteDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see VoteService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public Vote getVote(final String voteId){
		Vote vote = voteDao.findById(voteId);
		voteDao.initialize(vote);
		voteDao.initialize(vote.getSite());
		return vote;
	}
	
	/**
	 * @see VoteService#delete(String)
	 */
	@Transactional
	public void delete(final Vote vote) {
		voteDao.makeTransient(vote);
	}
	
	/**
	 * @see VoteService#save(OperLog)
	 */
	@Transactional
	public void save(final Vote vote) {
		voteDao.makePersistent(vote);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see VoteService#getPage(VoteBean)
	 */
	@Transactional(readOnly=true)
	public Page<Vote> getPage(final VoteBean voteBean){
		Page<Vote> page =  new Page<Vote>();
		page.setTotal(voteDao.count(voteBean));
		page.setResult(voteDao.findByQc(voteDao.assembly(voteBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see VoteService#list(Page, VoteBean)
	 */
	public void list(Page<Vote> page, VoteBean voteBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(voteDao.count(voteBean));
		}
		page.setResult(voteDao.findByPage(page,voteBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see VoteService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			voteDao.makeTransient(voteDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				voteDao.clear();
		}
		
	}
	/**
	 * 批量启用
	 * @param keys
	 */
	public void enable(String[] keys){
		if(keys!=null){
			for(String str: keys){
				Vote a=voteDao.findById(str);
				a.setVoteStatus(true);
				voteDao.makePersistent(a);
			}
		}
		
	}
	/**
	 * 批量禁用
	 * @param keys
	 */
	public void disable(String[] keys){
		if(keys!=null){
			for(String str:keys){
				Vote a=voteDao.findById(str);
				a.setVoteStatus(false);
				voteDao.makePersistent(a);
			}
		}
	}
	/**
	 * 检查唯一性
	 * @param role
	 * @return
	 */
	public Boolean checkNameUnique(String voteTitle,String votename,String siteId){
		return voteDao.checkNameUnique(voteTitle,votename,siteId);
	}

	public List<Vote> voteList(String siteId) {
		return voteDao.voteList(siteId);
	}
	/**
	 * 根据站点ID查询
	 * @return
	 */
	public List<Vote>findBySiteId(String siteId){
		return voteDao.findBySiteId(siteId);
	}
	public List<Vote>findBySiteId2(String siteId,String voteId){
		return voteDao.findBySiteId2(siteId,voteId);
	}
}